package com.drinks.yiniang.ui.activity.common;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.common.UpdateVersionJson;
import com.drinks.yiniang.io.api.CommonApiIO;
import com.drinks.yiniang.ui.activity.camera.AlbumActivity;
import com.drinks.yiniang.utils.GlideUtils;
import com.drinks.yiniang.utils.VersionUtils;
import com.hansen.library.Constants;
import com.drinks.yiniang.ui.activity.MainActivity;
import com.drinks.yiniang.ui.activity.user.login.LoginActivity;
import com.drinks.yiniang.utils.SharedPreUtils;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.StringUtils;

/**
 * 启动页面
 *
 * @author han
 * @date 2019/09/08
 */
public class WelcomeActivity extends AppCompatActivity {

    private static final int FLAG_NEXT = 0x01;
    private static final int FLAG_UPDATE = 0x02;
    private static final int DELAY_MILLIS = 3000;//延迟时间

    private Context mContext;

    //广告图
    private ImageView iv_welcome_ad;

    private String mDownloadUrl;
    private String mVersionName;

    //是否已经引导过用户
    private boolean mIsGuided;

    // 是否已经启动过
    private static boolean isLaunching = false;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == FLAG_NEXT) {
                showMainActivity();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        // 从堆栈恢复，不再重复解析之前的intent
        if (savedInstanceState != null) {
            setIntent(new Intent());
        }

        mContext = this;

        iv_welcome_ad = findViewById(R.id.iv_welcome_ad);

        getLaunchAd();

        initData();
    }

    private void initData() {
        mIsGuided = SharedPreUtils.getInstance().isAppGuided();

        if(isLaunching) {
            onIntent(); // APP进程还在，Activity被重新调度起来
        } else {
            // APP进程重新起来
            isLaunching = true;

            // 首次进入，打开欢迎界面
            mHandler.sendEmptyMessageDelayed(FLAG_NEXT, DELAY_MILLIS);

            //版本检测
            checkVersion();
        }
    }

    private void onIntent() {
        showMainActivity();
    }

    private void showMainActivity() {
        Intent mIntent = new Intent();

//        if(!mIsGuided) {
//            mIntent.setClass(WelcomeActivity.this, GuideActivity.class);
//        } else {
//            if(StringUtils.isEmpty(mUserToken)) {
//                mIntent.setClass(WelcomeActivity.this, LoginActivity.class);
//            } else {
//                if(StringUtils.isHttp(mDownloadUrl)) {
//                    mIntent.putExtra(Constant.KeyName, mVersionName);
//                    mIntent.putExtra(Constant.KeyUrl, mDownloadUrl);
//                }
//                mIntent.setClass(WelcomeActivity.this, MainActivity.class);
//            }
//        }

        if(StringUtils.isHttp(mDownloadUrl)) {
            mIntent.putExtra(Constants.KeyName, mVersionName);
            mIntent.putExtra(Constants.KeyUrl, mDownloadUrl);
        }

        mIntent.setClass(WelcomeActivity.this, MainActivity.class);

        startActivity(mIntent);

        finish();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.clear();
    }

    private void getLaunchAd() {
        CommonApiIO.getInstance().getLaunchAd(new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                if(iv_welcome_ad != null && !StringUtils.isEmpty(res.getData())) {
                    //GlideUtils.load(mContext, iv_welcome_ad, res.getData());
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {}

            @Override
            public void onComplete() {}
        });
    }

    //检测更新
    private void checkVersion() {
        CommonApiIO.getInstance().doAppUpdate(VersionUtils.getVersionCode(), new APIRequestCallback<UpdateVersionJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(UpdateVersionJson res) {
                if(res.getData().isNeed_update() && res.getData().getVersion_info() != null) {//需要更新
                    mVersionName = res.getData().getVersion_info().getVersion_name();
                    mDownloadUrl = res.getData().getVersion_info().getDownload_url();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        super.onDestroy();
    }
}
